package com.joysuch.wwyt.bp.controller;

import com.joysuch.wwyt.bp.bean.CurrentUserCompanyBean;
import com.joysuch.wwyt.bp.bean.risk.BpCompanyBean;
import com.joysuch.wwyt.bp.bean.risk.BpCompanyDto;
import com.joysuch.wwyt.bp.bean.risk.BpTeamSpecialBean;
import com.joysuch.wwyt.bp.entity.risk.BpCompany;
import com.joysuch.wwyt.bp.service.BpRiskCompanyService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.support.Context;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDate;

/**
 * @Author ZhangDong
 * @Date 2020/3/16 11:02
 */
@RestController
@RequestMapping("/bp/risk/company")
@Api(tags = "/bp/risk/company 风险研判-公司级")
@CrossOrigin
public class BpRiskCompanyController {

    @Autowired
    private BpRiskCompanyService bpRiskCompanyService;

    @PostMapping("save")
    @RequiresPermissions("bp:risk:company:save")
    @ApiOperation(value = "发布公告/修改")
    public ResultBean save(@RequestBody @Valid BpCompanyBean bpCompanyBean) {
        return bpRiskCompanyService.save(bpCompanyBean);
    }

    @GetMapping("release")
    @ApiOperation(value = " 特种作业统计", response = BpTeamSpecialBean.class)
    public ResultBean showData(@RequestParam(value = "announceTime", required = false)
                               @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate announceTime) {
        return bpRiskCompanyService.showData(announceTime);
    }


    @GetMapping("get/{id}")
    @ApiOperation(value = "根据id查询单条数据", response = BpCompanyDto.class)
    public ResultBean getById(@PathVariable Long id) {
        return bpRiskCompanyService.getById(id);
    }


    @GetMapping("page")
    @ApiOperation(value = "分页查询 announceTime: 公告日期", response = BpCompany.class)
    public ResultBean page(Pageable pageable, @RequestParam(value = "announceTime", required = false)
    @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate announceTime) {
        return bpRiskCompanyService.page(pageable, announceTime);
    }

    @GetMapping("delete/{id}")
    @RequiresPermissions("bp:risk:company:delete")
    @ApiOperation(value = "删除")
    public ResultBean delete(@PathVariable Long id) {
        return bpRiskCompanyService.delete(id);
    }

    @GetMapping("getTenentIdOrgCode")
    public ResultBean getOrgCode() {
        return ResultBean.success(new CurrentUserCompanyBean(Context.getCurrentTenantId(), Context.getCurrentOrgCode()));
    }

    @GetMapping("getAnnounce")
    public ResultBean getAnnounceMsg() {
        return ResultBean.success(bpRiskCompanyService.getAnnounceMsg());
    }
}
